package com.fyzk.activity.myview;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.annotation.Px;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import com.tyjc.lottery.R;
import com.fyzk.utils.AppUtil;
import com.fyzk.utils.ScreenUtils;

/**
 * 数字彩下拉展示(旧的)
 */
public class DrawBallFrameLayoutOld extends FrameLayout{

	private int pmW,pmH,pmD1;
	private float pmD2;

	private int x = 0, y = 0 , moveY = 0, lastY = 0;
	private Rect rect = new Rect(0, 0, 0, 0);
	private Bitmap qiu_bg_p;

	private BallScrollView scr;
	private BallPopFrameLayout fl_moveqiu;

	/**
	 * 滑动的最低端
	 */
	public int downY = 0;

	private boolean scr_isHuaDong = false;

	/**
	 * 上下滑动偏移量
	 */
	private int pianyiliang;

	public static final int STATE_TOP = 0;
	public static final int STATE_MOVE = 1;
	public static final int STATE_DOWN = 2;

	/**
	 * 移动状态
	 */
	public int state = 0;

	/**
	 * 是否是由上向下滑
	 */
	private boolean upToDown = true;

	private UpOrDownListener upOrDownListener;

	/**
	 * 构造函数
	 * @param context
	 * @param attrs
	 */
	public DrawBallFrameLayoutOld(Context context, AttributeSet attrs) {
		super(context, attrs);
		if (isInEditMode()) {
			return;
		}
		setWillNotDraw(false);



		this.pmW = AppUtil.getWidth();
		this.pmH = AppUtil.getHeight();
		this.pmD1 = AppUtil.getDensityDpi();
		this.pmD2 = AppUtil.getDensity();

		downY = AppUtil.getHeight()*200/560;
		pianyiliang = AppUtil.getHeight()*100/560;
		qiu_bg_p = BitmapFactory.decodeResource(context.getResources(), R.drawable.hongqiu_check);
		
		scr = new BallScrollView(context);
		fl_moveqiu = new BallPopFrameLayout(context, attrs);
	}
	
	/**
	 * 拦截触屏事件
	 */
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			y = (int) ev.getY();
			x = (int) ev.getX();
			return false;
		case MotionEvent.ACTION_MOVE:

			if (!scr.isHDTop()) {

				if (Math.abs(y - (int)ev.getY()) > 3 || Math.abs(x - (int)ev.getX()) > 20) {
					Rect rect = new Rect(0, 0, 0, 0);
					fl_moveqiu.setRect(rect, "",0,false);
				}
				return false;
			}
			if (Math.abs(y - (int)ev.getY()) > 3 || Math.abs(x - (int)ev.getX()) > 20) {
				Rect rect2 = new Rect(0, 0, 0, 0);
				fl_moveqiu.setRect(rect2, "",0,false);
				if (state == STATE_TOP && y < (int)ev.getY()) {
					return true;
				}else if(state == STATE_DOWN && y > (int)ev.getY()){
//					hua_fl = true;
//					hua_scrl = false;
					return true;

				}else{
					return false;
				}
			}else {
				return false;
			}



		case MotionEvent.ACTION_UP:
			return false;
		default:
			break;
		}

		return super.onInterceptTouchEvent(ev);
	}

	private boolean isClickDownLine = false;
	/**
	 * 触屏事件
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		isClickDownLine = false;
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
//			Log.e("pl move11111", "y: "+y+"  gety: "+(int)event.getY());
			float x2 = event.getX();
			float y2 = event.getY()+this.getScrollY();
			Rect rect = new Rect(0,0, ScreenUtils.getInstance().getWidth(), ScreenUtils.getInstance().getWidth()/13);
			if (rect.contains((int)x2,(int)y2)) {
				if (state == STATE_TOP) {
					upToDown = false;
					state = STATE_DOWN;
					lastY = -downY;
					this.scrollTo(0, -downY);
				} else if (state == STATE_DOWN) {
					upToDown = true;
					state = STATE_TOP;
					lastY = 0;
					this.scrollTo(0, 0);
				}
				isClickDownLine = true;
				break;
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if (isClickDownLine) {
				break;
			}
			if (Math.abs(y - (int)event.getY()) > 10) {
				state = STATE_MOVE;
				moveY = (int)event.getY();
				
				if (upToDown) {//向下移
					
					if (moveY < y) {
						y = moveY;
						return false;
					}
					
					if (moveY - y > pianyiliang) {

						upToDown = false;
						state = STATE_DOWN;
						lastY = -downY;
						y = moveY;
						this.scrollTo(0, -downY);
						break;
					}
				}else {//向上移
					
					if (moveY > y) {
						y = moveY;
					}
					
					if (y - moveY >pianyiliang) {

						upToDown = true;
						state = STATE_TOP;
						lastY = 0;
						y = moveY;
						this.scrollTo(0, 0);
						break;
					}
				}
				this.scrollTo(0, lastY + y - moveY);
			}
			
			break;
		case MotionEvent.ACTION_UP:
			if (isClickDownLine) {
				break;
			}
			if (upToDown) {
				if (moveY - y < pianyiliang) {
					state = STATE_TOP;
					lastY = 0;
					this.scrollTo(0, 0);
				}
			}else {
				if (y - moveY < pianyiliang) {
					state = STATE_DOWN;
					lastY = -downY;
					this.scrollTo(0, -downY);
				}
			}
			break;
		default:
			break;
		}
		
		return true;
	}

	@Override
	public void scrollTo(@Px int x, @Px int y) {
		super.scrollTo(x, y);
		if (upOrDownListener != null) {
			upOrDownListener.upOrDownState(state);
		}
	}

	@Override
	public void setOnTouchListener(OnTouchListener l) {
		// TODO Auto-generated method stub
		super.setOnTouchListener(l);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}

	public int getY1() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getX1() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public Rect getRect() {
		return rect;
	}

	public void setRect(Rect rect) {
		this.rect = rect;
		this.invalidate();
	}

	public BallScrollView getScr() {
		return scr;
	}

	public void setScr(BallScrollView scr) {
		this.scr = scr;
	}

	public BallPopFrameLayout getFl_moveqiu() {
		return fl_moveqiu;
	}

	public void setFl_moveqiu(BallPopFrameLayout fl_moveqiu) {
		this.fl_moveqiu = fl_moveqiu;
	}

	public UpOrDownListener getUpOrDownListener() {
		return upOrDownListener;
	}

	public void setUpOrDownListener(UpOrDownListener upOrDownListener) {
		this.upOrDownListener = upOrDownListener;
	}

	public interface UpOrDownListener {
		void upOrDownState(int state);
	}
}
